package com.weepal.pavement.mqtt.demo.config.dataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.Map;

@Configuration
@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
public class DataSourceConfig {

    @Autowired
    private NacosDynamicsDataSources dataSources;

    @Bean("dynaDatasource")
    @Primary
    public DataSource dynaDatasource() {
        Map<Object, Object> ds = dataSources.buildSource();
        DynamicsDataSource dynamicsDataSource = new DynamicsDataSource();
        dynamicsDataSource.setDefaultTargetDataSource(ds.get("master"));//默认数据源
        dynamicsDataSource.setTargetDataSources(ds);//其他数据源
        return dynamicsDataSource;
    }

    public static class Constant {
        public static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

        public static final String MASTER = "master";

        public static final String HUAIYANG = "huaiyang";
    }
}
